package com.marvels.financial.loan.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.DateUtils;
import com.marvels.financial.loan.dao.FinanceStaticDao;
import com.marvels.financial.loan.dto.AnalysisStatisticalConditionDTO;
import com.marvels.financial.loan.dto.BorrowStaticDTO;
import com.marvels.financial.loan.dto.PaymentStaticDTO;
import com.marvels.financial.loan.service.FinanceStaticApi;

import cn.hutool.core.util.StrUtil;

@Service
public class FinanceStaticImpl implements FinanceStaticApi{

	@Autowired
	private FinanceStaticDao financeStaticDao;
	
	@Override
	public List<BorrowStaticDTO> borrowStatic(AnalysisStatisticalConditionDTO conditionDTO)
			throws MarvelsBusinessException {
		if(StrUtil.isEmpty(conditionDTO.getBegindDate())){//开始时间为空
			throw new MarvelsBusinessException("请选择开始日期");
		}
		if(StrUtil.isEmpty(conditionDTO.getEndDate())){
			throw new MarvelsBusinessException("请选择结束日期");
		}
		Date beginDate= DateUtils.getDateYYYY_MM_DD(conditionDTO.getBegindDate());
		Date enDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getEndDate());
		List<BorrowStaticDTO>   list=new ArrayList<BorrowStaticDTO>();
		for(Date tmp=beginDate;enDate.compareTo(tmp)>=0;tmp=DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)){
			conditionDTO.setBegindDate(DateUtils.getDateYYYY_MM_DD(tmp));
			conditionDTO.setEndDate(DateUtils.getDateYYYY_MM_DD(DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)));
			BorrowStaticDTO  tmpDTO = financeStaticDao.borrowStatic(conditionDTO);
			tmpDTO.setQueryDate(tmp);
			if(tmpDTO.getBorrowAmount().compareTo(BigDecimal.ZERO)==0){//借款金额等于0
				tmpDTO.setLoanAmountPassRate(BigDecimal.ZERO);
			}else{
				tmpDTO.setLoanAmountPassRate(tmpDTO.getLoanAmount().divide(tmpDTO.getBorrowAmount(),3,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
			}
		
			if(tmpDTO.getBorrowCount()==0){//借款笔数等于0
				tmpDTO.setLoanPassRate(BigDecimal.ZERO);
			}else{
				tmpDTO.setLoanPassRate(new BigDecimal(tmpDTO.getLoanCount()).divide(new BigDecimal(tmpDTO.getBorrowCount())).setScale(3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
			}
			list.add(tmpDTO);
		}
		return list;
	}

	@Override
	public List<PaymentStaticDTO> paymentStatic(AnalysisStatisticalConditionDTO conditionDTO)
			throws MarvelsBusinessException {
		if(StrUtil.isEmpty(conditionDTO.getBegindDate())){//开始时间为空
			throw new MarvelsBusinessException("请选择开始日期");
		}
		if(StrUtil.isEmpty(conditionDTO.getEndDate())){
			throw new MarvelsBusinessException("请选择结束日期");
		}
		Date beginDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getBegindDate());
		Date enDate=DateUtils.getDateYYYY_MM_DD(conditionDTO.getEndDate());
		List<PaymentStaticDTO>   list=new ArrayList<PaymentStaticDTO>();
		for(Date tmp=beginDate;enDate.compareTo(tmp)>=0;tmp=DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)){
			conditionDTO.setBegindDate(DateUtils.getDateYYYY_MM_DD(tmp));
			conditionDTO.setEndDate(DateUtils.getDateYYYY_MM_DD(DateUtils.add(tmp, Calendar.DAY_OF_YEAR, 1)));
			PaymentStaticDTO  tmpDTO = financeStaticDao.paymentStatic(conditionDTO);
			tmpDTO.setQueryDate(tmp);
			if(tmpDTO.getPaymentAmount().compareTo(BigDecimal.ZERO)==0){//还款金额等于0
				tmpDTO.setOverduePaymentAmountRate(BigDecimal.ZERO);
			}else{
				tmpDTO.setOverduePaymentAmountRate(tmpDTO.getOverduePaymentAmount().divide(tmpDTO.getPaymentAmount(),3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
			}
		
			if(tmpDTO.getPaymentCount()==0){//还款笔数等于0
				tmpDTO.setOverduePaymenRate(BigDecimal.ZERO);
			}else{
				tmpDTO.setOverduePaymenRate((new BigDecimal(tmpDTO.getOverduePaymentCount()).divide(new BigDecimal(tmpDTO.getPaymentCount()),3, BigDecimal.ROUND_HALF_UP)).setScale(3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
			}
			list.add(tmpDTO);
		}
		return list;
	}
	
	
	public static void main(String[] args) {
		BigDecimal b1=new BigDecimal("2");
		BigDecimal b2=new BigDecimal("3500");
		System.out.println(b1.divide(b2,3, BigDecimal.ROUND_HALF_UP));
	}
}
